package com.tudouji.project.store.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.flow.service.mapper.IFlowPermissionAssignMapper;
import com.flow.service.pojo.flow.FlowPermissionAssign;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.bill.domain.Whinform;
import com.tudouji.project.dept.domain.DeptStoreroom;
import com.tudouji.project.dept.mapper.DeptStoreroomMapper;
import com.tudouji.project.permission.domain.FlowPermissionAssignDO;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import com.tudouji.project.system.domain.SysUser;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.store.mapper.StoreroomMapper;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStoreroomService;

/**
 * 库房管理Service业务层处理
 * 
 * @author tudouji
 * @date 2021-01-04
 */
@Service
public class StoreroomServiceImpl implements IStoreroomService 
{
    @Autowired
    private StoreroomMapper storeroomMapper;
    @Autowired
    private DeptStoreroomMapper deptStoreroomMapper;
    @Autowired
    private ISalespermissionService permissionService;

    /**
     * 查询库房管理
     * 
     * @param guid 库房管理ID
     * @return 库房管理
     */
    @Override
    public Storeroom selectStoreroomById(Long guid)
    {
        return storeroomMapper.selectStoreroomById(guid);
    }
    @Override
    public Storeroom selectStoreroomByGuid(String guid)
    {
        return storeroomMapper.selectStoreroomByGuid(guid);
    }

    /**
     * 查询库房管理列表
     * 
     * @param storeroom 库房管理
     * @return 库房管理
     */
    @Override
    public List<Storeroom> selectStoreroomList(Storeroom storeroom)
    {
        return storeroomMapper.selectStoreroomList(storeroom);
    }

    /**
     * 新增库房管理
     * 
     * @param storeroom 库房管理
     * @return 结果
     */
    @Override
    public int insertStoreroom(Storeroom storeroom)
    {
        storeroom.setGuid(IdUtils.fastSimpleUUID());
        storeroom.setOperatorid(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        storeroom.setCreatedate(DateUtils.getNowDate());
        storeroom.setLastupdate(DateUtils.getNowDate());
        return storeroomMapper.insertStoreroom(storeroom);
    }

    /**
     * 修改库房管理
     * 
     * @param storeroom 库房管理
     * @return 结果
     */
    @Override
    public int updateStoreroom(Storeroom storeroom)
    {
        storeroom.setLastupdate(DateUtils.getNowDate());
        return storeroomMapper.updateStoreroom(storeroom);
    }

    /**
     * 批量删除库房管理
     * 
     * @param guids 需要删除的库房管理ID
     * @return 结果
     */
    @Override
    public int deleteStoreroomByIds(String[] guids)
    {
        return storeroomMapper.deleteStoreroomByIds(guids);
    }

    /**
     * 删除库房管理信息
     * 
     * @param guid 库房管理ID
     * @return 结果
     */
    @Override
    public int deleteStoreroomById(String guid)
    {
        return storeroomMapper.deleteStoreroomById(guid);
    }

    @Override
    public Storeroom selectSaleTransferStore(String orgId) {
        return storeroomMapper.selectSaleTransferStore(orgId);
    }

    /**
     * 查询库房管理列表
     *
     * @param storeroom 库房管理
     * @return 库房管理
     */
    @Override
    public List<Storeroom> selectStoreroomListByPermiss(Storeroom storeroom, String tuserId) throws Exception {
        // 查询用户是否有全局权限
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(tuserId, storeroom.getFunctionType(), "4");
        int flag = 0;
        if (permissions.size() < 1) {// 没有全局权限
            // 渠道
            List<Salespermission> srs = permissionService.getPermByScAndFunc(tuserId, storeroom.getFunctionType(), "7");
            if (srs.size() > 0) {
                flag = 1;
                String[] srGuids = new String[srs.size()];
                for (int i = 0; i < srs.size(); i++) {
                    srGuids[i] = srs.get(i).getSrguid();
                }
                storeroom.setGuids(srGuids);
                flag = 1;
            }
        } else {
            flag = 1;
        }
        if (flag == 1) {
            return storeroomMapper.selectStoreroomList(storeroom);
        }
        return new ArrayList<Storeroom>();
    }

    @Override
    public List<Storeroom> getStoreRoomByOrgIdOrPermissions(String guid, Integer enable, Integer type
            , Integer send
            , Integer flag
            , Integer permissionFlag
            , String functionType
            , Integer permissionId) {

        Storeroom storeroom = new Storeroom();
        if (StringUtils.isNotBlank(guid)){
            storeroom.setOrgguid(guid);
        }

        if (null != enable){
            storeroom.setEnable(enable);
        }
        if (null != type){
            storeroom.setType(type);
        }else{
            storeroom.setType(1);
        }
        if (null != send){
            storeroom.setSend(send);
        }
        if (null != flag){
            storeroom.setFlag(flag);
        }

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();

        if (null != permissionFlag && 1 == permissionFlag) {
            //查询是否有全局权限
            List<Salespermission> permissions = permissionService.getPermByScAndFuncSYS(sysUser.getUserId(), functionType
                    , "4");
            if (permissions.size() > 0){
                return storeroomMapper.selectStoreroomByCommon(storeroom);
            }else{
                // 是否有组织权限
                List<Salespermission> permissionsOrg = permissionService
                        .getPermByScAndFuncSYS(sysUser.getUserId(), functionType, "7");
                if (permissionsOrg.size() > 0){
                    String[] roomids = new String[permissionsOrg.size()];
                    for (int i = 0; i < permissionsOrg.size(); i++) {
                        roomids[i] = permissionsOrg.get(i).getSrguid();
                    }
                    storeroom.setRoomids(roomids);
                }
            }

        }
        if (null != permissionFlag && 2 == permissionFlag) {
            storeroom.setPermissionId(permissionId);
            storeroom.setCreateBy(sysUser.getUserId());
            return permissionService.getUserOfSrbyPermssion(storeroom);
        }
        return storeroomMapper.selectStoreroomByCommon(storeroom);

    }

    @Override
    public List<Storeroom> getStoreRoomsByCondition(String orgguid, Long deptId, Integer innerOut) {

        Storeroom storeroom = new Storeroom();
        if (StringUtils.isNotBlank(orgguid)){
            storeroom.setOrgguid(orgguid);
        }
        if (StringUtils.isNotNull(deptId)){
            storeroom.setDeptId(deptId);
        }
        if (StringUtils.isNotNull(innerOut)){
            storeroom.setInnerOut(innerOut);
        }
        return storeroomMapper.selectStoreroomByCommon(storeroom);

    }

    @Override
    public List<Storeroom> findSRByPermiss(Integer permiss) {
        List<Storeroom> list=new ArrayList<>();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Salespermission permission=new Salespermission();
        permission.setUserId(sysUser.getUserId());
        permission.setPermission(permiss);
        List<Salespermission> salespermissions = permissionService.selectSalespermissionList(permission);
        if(salespermissions!=null){
            String[] guids=new String[salespermissions.size()];
            for(int i=0;i<salespermissions.size();i++){
                guids[i]=salespermissions.get(i).getSrguid();
            }
            Storeroom room=new Storeroom();
            room.setGuids(guids);
            list=storeroomMapper.selectStoreroomList(room);
        }
        return list;
    }
}
